package com.lipuwater.agent.ui.activity;

import android.graphics.Color;

import com.ismartlib.utils.NetUtils;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.adapter.BaseListAdapter;
import com.lipuwater.agent.app.base.ui.BaseListActivity;
import com.lipuwater.agent.http.ApiCallBack;
import com.lipuwater.agent.http.HttpConfig;
import com.lipuwater.agent.http.ResponseParams;
import com.lipuwater.agent.http.api.ApiRequestsBasis;
import com.lipuwater.agent.http.bean.account.AccountInfoBean;
import com.lipuwater.agent.http.bean.account.AccountListBean;
import com.lipuwater.agent.ui.adapter.ATAccountAdapter;
import com.lipuwater.agent.utils.MapToBeanUtil;
import com.lipuwater.agent.utils.PriceUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class AccountActivity extends BaseListActivity<AccountInfoBean> {
    private int PAGE_INDEX = 1;
    private int PAGE_SIZE = 10;
    private boolean isShowLoading = false;
    private ATAccountAdapter accountAdapter;

    @Override
    protected void OnViewCreated() {
        toolbar.setBackgroundResource(R.drawable.img_title_bar);
        initTitle(R.drawable.icon_aleft_white, "我的账户");
        toolbarTitle.setTextColor(Color.parseColor("#ffffff"));
        showLoading();
    }

    @Override
    public void requestData(boolean needRefresh) {
        if (needRefresh) {
            PAGE_INDEX = 1;
        }
        if (!isShowLoading) {
            isShowLoading = true;
            showLoading();
        }
        getAccountList();
    }

    @Override
    protected BaseListAdapter<AccountInfoBean> getListAdapter() {
        accountAdapter = new ATAccountAdapter(this);
        return accountAdapter;
    }

    private void getAccountList() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", token);
        params.put("pageIndex", PAGE_INDEX);
        params.put("pageSize", PAGE_SIZE);
        ApiRequestsBasis.getAccount(this, params, new ApiCallBack() {
            @Override
            public void onResponse(ResponseParams result) {
                if (result.getStatus() == HttpConfig.success) {
                    setData(result);
                    if (isShowLoading) {
                        hideEmptyLayout();
                    }
                } else {
                    if (isShowLoading) {
                        isShowLoading = false;
                        showNoNetWork();
                    }
                    Toast(result.getMessage());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                if (isShowLoading) {
                    isShowLoading = false;
                    showNoNetWork();
                }
                onDataLoaded(null);//没网络记得调用onDataLoaded，否则导致刷新被禁止
                if (!NetUtils.isConnected(AccountActivity.this)) {
                    Toast(getString(R.string.network_fails));
                } else {
                    Toast(getString(R.string.service_error));
                }
            }
        });
    }

    private void setData(ResponseParams result) {
        AccountListBean accountList = (AccountListBean) MapToBeanUtil.toJavaBean(result.getParams(), AccountListBean.class);
        if (accountAdapter != null && accountAdapter.mHeadText != null) {
            accountAdapter.mHeadText.setText(PriceUtils.changeF2Y(accountList.getPrice()));
        }
        if (accountList != null && accountList.getList().size() > 0) {
            PAGE_INDEX++;
            onDataLoaded(accountList.getList());
        } else {
            onDataLoaded(null);
        }
    }

}
